<?php

/**
* alltosun.com 优惠静态方法 index.php
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 吴松键 (wusj@alltosun.com) $
* $Date: 2014-8-8 25 上午10:36:18 $
* $Id$
*/

class coupon_helper
{
    /**
     * 获取通用优惠券的信息
     */
    public static function get_general_coupon_info($goods_id, $code = NULL)
    {
        $map = coupon_config::general_coupon_goods_map();
        if(!isset($map[$goods_id])) {
            return array();
        }
        if(NULL === $code) {
            return $map[$goods_id];
        }
        if(!isset($map[$goods_id][$code])) {
            return array();
        }
        return $map[$goods_id][$code];
    }

    /**
     * 获取当前优惠券是否在有效期内
     * @return int 1:有效 0:无效
     */
    public static function get_is_valid($coupon_id)
    {
        if (!$coupon_id) {
            return 0;
        }

        $coupon_info = _uri('coupon', $coupon_id);
        $start_time  = strtotime($coupon_info['start_time']);
        $end_time    = strtotime($coupon_info['end_time']);
        $curr_time   = time();
        if ($curr_time >= $start_time && $curr_time <= $end_time) {
            return 1;
        }

        return 0;
    }

    /**
     * 根据当前优惠券的编号，判断优惠券是否可以使用，如果可以使用返回价格。不能使用返回原因
     * @param string $coupon_code 优惠券编码
     * @return array() 'info' 原因  offer_price 优惠价格
     */
    public static function check_is_available($coupon_code, $check_user_id = 0)
    {
        if (!$coupon_code) {
            return array('info'=>'无效编码');
        }

        $coupon_relation_info = _uri('coupon_relation', array('coupon_code'=>$coupon_code));
        if (empty($coupon_relation_info)) {
            return array('info'=>'当前优惠券不存在');
        }
        $is_available = coupon_helper::get_is_valid($coupon_relation_info['coupon_id']);
        if (!$is_available) {
            return array('info'=>'当前优惠券不在使用期限内');
        }

        if ($check_user_id == 1 && ($coupon_relation_info['user_id'] > 0 && $this->user_id != $coupon_relation_info['user_id'])) {
            return array('info'=>'你没有权限使用该优惠券');
        }
        $coupon_info = _uri('coupon', $coupon_relation_info['coupon_id']);
        if (!$coupon_info['status']) {
            return array('info'=>'当前优惠券不存在');
        }

        return array(
            'info'=>'ok',
            'options_type' => $coupon_info['options_type'],
            'category_id'=>$coupon_info['category_id'],
            'goods_id'   =>$coupon_info['goods_id'],
            'is_all_user'=>$coupon_info['is_all_user'],
            'offer_price'=>$coupon_info['offer_price'],
            'limit_price'=>$coupon_info['limit_price'],
            'status'=>$coupon_relation_info['status']
        );
    }

    /**
     * 获取当前商品是否可使用优惠券
     */
    public static function goods_in_coupon($goods_id, $coupon_code)
    {
        if (!$coupon_code || !$goods_id) {
            return 0;
        }

        $coupon_id = _uri('coupon_relation', array('coupon_code'=>$coupon_code), 'coupon_id');
        if (!$coupon_id) {
            return 0;
        }
        $coupon_info = _uri('coupon', $coupon_id);
        if (!$coupon_info) {
            return 0;
        }
        if($coupon_info['goods_id'] == 0) {
            $curr_category_id = _uri('goods', $goods_id, 'category_id');
            if($coupon_info['category_id'] == $curr_category_id) {
                return 1;
            } else {
                return 0;
            }
        } else {
            if($coupon_info['goods_id'] == $goods_id) {
                return 1;
            } else {
                return 0;
            }
        }
        

        return 0;
    }

    /**
     * 获取优惠券使用条件
     */
    public static function check_is_limit($category_id, $goods_id, $limit_price, $cart_list = array())
    {
        // liw mod 直接购买的商品传递进来
        if(!$cart_list) {
            if(!isset($_SESSION['order_pay_account'])) {
                $cart_list = _widget('cart')->get_list();
            } else {
                $pay_account_id      = (int)$_SESSION['order_pay_account'];
                $alipay_account_list = Config::get('alipay_account_list');
                if(!array_key_exists($pay_account_id, $alipay_account_list)) {
                    return '当前购物车内没有商品';
                }
                $category_cart_list = cart_helper::category_cart_by_alipay_account();
                $cart_list = $category_cart_list[$pay_account_id];
            }
        }
        if (empty($cart_list)) {
            return '当前购物车内没有商品';
        }
        $price_limit_price = 0;
        $in_coupon = 0;
        if($goods_id) {
            foreach ($cart_list as $k=>$v) {
                if($goods_id == $v['goods_id']) {
                    $price_limit_price += $v['goods_price'] * $v['goods_num'];
                    $in_coupon = 1;
                }
            }
        } else {
            foreach ($cart_list as $k=>$v) {
                $goods_id = $v['goods_id'];
                $curr_category_id = _uri('goods', $goods_id, 'category_id');
                $category_ids = category_helper::get_all_ids($curr_category_id);
                if (in_array($category_id, $category_ids)) {
                    $price_limit_price += $v['goods_price'] * $v['goods_num'];
                    $in_coupon = 1;
                }
            }
        }
        

        if (!$in_coupon) {
            return '该优惠券不满足使用条件';
        }
        if ($price_limit_price < $limit_price) {
            return '满足该优惠券的商品不足￥'.$limit_price;
        }

        return 'ok';
    }

    /**
     * 根据优惠券code 查询优惠券价格
     */
    public function get_coupon_price($coupon_code)
    {
        if (!$coupon_code) {
            return 0;
        }

        $coupon_relation_info = _uri('coupon_relation', array('coupon_code'=>$coupon_code));
        if (empty($coupon_relation_info)) {
            return 0;
        }

        $coupon_price = _uri('coupon', $coupon_relation_info['coupon_id'], 'offer_price');
        if (!$coupon_price) {
            return 0;
        }

        return $coupon_price;
    }

    public function get_coupon_use_price($coupon_code)
    {
        // 是否可用
        $info = coupon_helper::check_is_available($coupon_code, 1);
        if ($info['info'] != 'ok') {
            return 0;
        }

        // 是否满足购物车
        $msg = coupon_helper::check_is_limit($info['category_id'], $info['goods_id'], $info['limit_price']);
        if ($msg != 'ok') {
            return 0;
        }

        return $info['offer_price'];
    }

    /**
     * 获取已过期优惠券ids
     */
    public static function get_coupon_ids($type)
    {
        if ($type == 'end_time') {
            $filter = array(
                '`end_time` <' => date('Y-m-d H:i:s', time()),
                'status' => 1
            );
        } elseif ($type == 'status') {
            $filter = array(
                'status' => 1
            );
        }

        $coupon_ids = _model('coupon')->getFields('id', $filter);
        if (!empty($coupon_ids)) {
            return $coupon_ids;
        }

        return '';
    }
}
?>